package com.mxl.quartz;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 配置执行 定时任务
 * @author MXL
 * @date 2021/10/13
 **/
@Configuration
public class QuartzConfig {

    /**
     * 配置JobDetail类
     * @return
     */
    @Bean
    JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(MyJob01.class);
        return jobDetailFactoryBean;
    }

    /**
     * 配置trigger绑定到哪个JobDetail上
     * @return
     */
    @Bean
    CronTriggerFactoryBean cronTriggerFactoryBean(){
        CronTriggerFactoryBean cronTriggerFactory = new CronTriggerFactoryBean();
        cronTriggerFactory.setJobDetail(jobDetailFactoryBean().getObject());
        cronTriggerFactory.setCronExpression("0/5 58 * * * ?");
        return cronTriggerFactory;
    }

    /**
     * 启动器 绑定 trigger
     * @return
     */
    @Bean
    SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();;
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject());
        return  schedulerFactoryBean;
    }
}
